
class Main extends egret.DisplayObjectContainer {
    
    public btn:Button;
    
    public constructor() {
        super();
        let shp:egret.Shape = new egret.Shape();
        this.addChild(shp);
        shp.graphics.beginFill(0xff0000);
        shp.graphics.drawRect(0,0,100,120)
        shp.graphics.endFill();
        
       // shp.graphics.clear();
        shp.graphics.lineStyle(1,0xff0000);
        shp.graphics.moveTo(100,100);
        shp.graphics.curveTo(150,50,200,100)
        shp.graphics.endFill();

        shp.graphics.beginFill(0x00ff00);
        shp.graphics.drawArc(200,200,100,0,Math.PI); //弧度为单位
        shp.graphics.endFill();
        
        console.log(shp.x,shp.y)
        let g:MyGrid = new MyGrid();
        this.addChild(g);
        let htp:egret.HttpRequest = new egret.HttpRequest();
        let clsName:string = egret.getQualifiedClassName(htp)
        console.log("clsName",clsName)
        
        let rect:egret.Shape = new egret.Shape();
        this.addChild(rect);
        rect.graphics.beginFill(0xcccccc);
        rect.graphics.drawRect(0,0,100,100);
        rect.graphics.endFill();
        rect.x = rect.y = 300;

        let r:number = 100;
        let progressShp:egret.Shape = new egret.Shape();
        progressShp.x = rect.x-rect.width/2;
        progressShp.y = rect.y-rect.height/2;
        this.addChild(progressShp)
        this.getArcProgress(progressShp);
        progressShp.mask = rect; //设置遮罩
        let game:com.game.Game = new com.game.Game();
        this.addChild(game)

        let txt:egret.TextField = new egret.TextField();
        this.addChild(txt);
        txt.text = "文本内容";
        txt.textColor = 0xff0000;
        txt.fontFamily = "KaiTi";
        //txt.width = txt.height = 300;
        txt.textAlign = egret.VerticalAlign.BOTTOM;
        txt.strokeColor = 0x00ff00; //描边
        txt.stroke = 1; //描边大小 1 像素
        txt.touchEnabled = true; //默认是不可点击的
        
        //自定义样式
        let flow:Array<egret.ITextElement> = new Array<egret.ITextElement>(
            {"text":"hello",style:{'textColor':0x000000,"size":50}},
            {"text":"world",style:{'textColor':0x00ff00,"size":60,"strokeColor":0xffff00}},
            {"text":"链接",style:{'href':"http://www.baidu.com"}},
            {"text":"事件",style:{'href':"event:1002|1003"}},

            );
       txt.textFlow = flow;
       flow = (new egret.HtmlTextParser()).parse(
           "没有任何格式的文本"+"<u><a href='http://www.baidu.com' >链接</a><u>"
       );
       console.log("flow",flow)
        //txt.textFlow = flow;
       //html文本的支持
       txt.addEventListener(egret.TextEvent.LINK,this.onLinkTxt,this)
        //txt.size = 16;
        console.log("txt.textWidth,txt.width=",txt.textWidth,txt.width,txt.size)
        
        this.btn = new Button("按钮");        
        this.btn.x = 20;
        this.btn.y = 350;
        this.addChild(this.btn);
        this.btn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouchBtn,this); //相当于click
        this.btn.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.onTouchBtn,this); //相当于mousedown
        this.btn.addEventListener(egret.TouchEvent.TOUCH_END,this.onTouchBtn,this); //相当于mouseup

        //加载图片
        let imgLdr:egret.ImageLoader = new egret.ImageLoader();
        imgLdr.once(egret.Event.COMPLETE,this.onImgLoaded,this);
        imgLdr.load("resource/bg.jpg");

        //再次熟悉画扇形
        let radius:number = 100;
        let shp2:egret.Sprite = new egret.Sprite();
        this.addChild(shp2);
        shp2.x = 300;
        shp2.y = 100;
        shp2.graphics.lineStyle(2,0xffff00);
        shp2.graphics.beginFill(0xff0000);
        //shp2.graphics.drawCircle(0,0,radius);
        //shp2.graphics.moveTo(0,0);
        shp2.graphics.lineTo(radius,0);
        shp2.graphics.drawArc(0,0,radius,0,Math.PI);
        shp2.graphics.lineTo(0,0);
        shp2.graphics.endFill();
        
    }

    private onImgLoaded(evt:egret.Event){
        let imgLdr:egret.ImageLoader = evt.currentTarget as egret.ImageLoader;
        console.log("imgLdr.data=",imgLdr.data);
        
        
        let bgBmp:egret.BitmapData = imgLdr.data;
        var te:egret.Texture = new egret.Texture();
        te.bitmapData = bgBmp;
        let bitmap:egret.Bitmap = new egret.Bitmap(te);
        this.addChild(bitmap);
        this.setChildIndex(bitmap,0);
    
    }

    private onTouchBtn(evt:egret.TouchEvent):void
    {
        console.log("...点击按钮...",evt.type);
    }
    private onLinkTxt(evt:egret.TextEvent):void
    {
        console.log("evt.text",evt.text)
    }
    private getArcProgress(shp:egret.Shape):egret.Shape {
        
        let angle:number = 0;
        egret.startTick(function (stmp:number):boolean
        {
            angle += 1;
            changeGraphics(angle);
            angle %= 360;
            return true;
        },this)
        
        function changeGraphics(angle){
            
            let r:number = 100;
            shp.graphics.clear();
            shp.graphics.lineStyle(2,0x00ff00);
            //shp.graphics.moveTo(r,r);
            shp.graphics.lineTo(2*r,0);
            shp.graphics.beginFill(0x00ff00,0.7);
            shp.graphics.drawArc(r,r,r,0,angle/180*Math.PI,true); //逆时针绘制 弧度制
            shp.graphics.lineTo(r,r);
            shp.graphics.endFill()
        }


        return shp;
    }

}

class Button extends egret.Sprite{
    lbl:egret.TextField;
    gap:number = 8;
    private _label:string;
    public constructor(labelText:string=""){
        super();
        this.lbl = new egret.TextField();
        this.lbl.textColor = 0x000000;
        this.addChild(this.lbl);
        this.lbl.x = this.gap/2;
        this.lbl.y = this.gap/2;
        this.touchEnabled = true;
        this.label = labelText;
    }

    public set label(value:string)
    {
        this._label = value;
        this.lbl.text = this._label;
        this.draw();
    }
    public get label():string
    {
        return this._label;
    }
    private draw(){
        this.graphics.clear();
        this.graphics.beginFill(0xcccccc);        
        this.graphics.drawRect(0,0,this.lbl.textWidth+this.gap,this.lbl.textHeight+this.gap);
        this.graphics.endFill();

    }
}